home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SunSoft Catalyst CDWARE 1996 May to August
/
Catalyst CDWARE 1996 May to August.iso
/
.products
/
FSA
/
classes
/
dance.java
next >
Wrap
Text File
|
1996-02-02
|
6KB
|
278 lines
/*
* @(#)DancingText.java 1.2 95/06/08 James Gosling
*
* Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for NON-COMMERCIAL purposes and without fee is hereby
* granted provided that this copyright notice appears in all copies. Please
* refer to the file "copyright.html" for further important copyright and
* licensing information.
*
* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
* SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
* OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
* LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR
* ITS DERIVATIVES.
*/
import java.io.InputStream;
import awt.*;
import browser.*;
import browser.audio.AudioData;
import net.www.html.*;
/**
* A simple Item class to play an image loop. The "img" tag parameter
* indicates what image loop to play.
*
* @author James Gosling
* @version 1.16, 10 Dec 1994
*/
public
class DancingText extends Applet implements Runnable {
char text[];
/**
* The thread animating the images.
*/
private Thread kicker = null;
private int time;
private int delay;
private int pause;
private int style;
private String audioName;
private int t0;
private boolean atStart = true;
private int x0[];
private int xd[];
private AudioData ad;
private boolean fetchingAudio;
private boolean playRequested;
/**
* Initialize the applet. Get attributes.
*/
public void init() {
try {
String s = null;
int W = width;
int H = height;
try {
s = getAttribute("text");
} catch(Exception e) {
}
if (s == null)
s = "Your message here";
text = new char[s.length() + 2];
s.getChars(0, s.length(), text, 0);
try {
time = Integer.parseInt(getAttribute("time"));
} catch(Exception e) {
time = 1000;
}
try {
audioName = getAttribute("sound");
} catch(Exception e) {
}
try {
delay = Integer.parseInt(getAttribute("delay"));
} catch(Exception e) {
delay = 50;
}
try {
pause = Integer.parseInt(getAttribute("pause"));
} catch(Exception e) {
pause = 0;
}
try {
style = Integer.parseInt(getAttribute("style"));
} catch(Exception e) {
style = 0;
}
String name = getAttribute("font");
if (name == null)
name = "TimesRoman36i";
int fstyle = 0;
int size = 0;
int pos = name.length();
int c = 0;
while (pos > 0) {
c = name.charAt(--pos);
switch (c) {
case 'b':
fstyle |= Font.BOLD;
continue;
case 'i':
fstyle |= Font.ITALIC;
continue;
case 'u':
fstyle |= Font.UNDERLINE;
continue;
}
break;
}
int fac = 1;
while ('0' <= c && c <= '9') {
size += (c - '0') * fac;
if (--pos <= 0)
break;
c = name.charAt(pos);
fac = fac * 10;
}
if (size <= 0)
size = 24;
name = name.substring(0, pos + 1);
Font f = getFont(name, fstyle, size);
if (f == null)
f = font;
else
font = f;
if (H < f.height)
H = f.height;
int len = text.length;
x0 = new int[len];
xd = new int[len];
int x = 0;
for (int i = 0; i < len; i++) {
xd[i] = x;
x += f.widths[text[i]];
}
if (x > W)
W = x;
if (W > x) {
int dx = (W - x) / 2;
for (int i = 0; i < len; i++)
xd[i] += dx;
}
resize(W, H);
switch (style) {
default:
case 1:
for (int i = 0; i < len; i++)
x0[i] = W / 2;
break;
case 2:
for (int i = 0; i < len; i++)
x0[i] = W;
break;
case 3:
for (int i = 0; i < len; i++)
x0[i] = 0;
break;
case 4:
for (int i = 0; i < len; i++)
x0[i] = xd[i] + W;
break;
case 5:
for (int i = 0; i < len; i++)
x0[i] = xd[len - i - 1];
break;
case 6:
for (int i = 0; i < len; i++)
x0[i] = (i & 1) == 0 ? W : 0;
break;
case 7:
for (int i = 0; i < len; i++)
x0[i] = (int) (W * Math.random());
break;
}
t0 = System.nowMillis();
atStart = true;
} catch(Exception e) {
e.printStackTrace();
}
}
/**
* Run the image loop. This methods is called by class Thread.
* @see java.lang.Thread
*/
public void run() {
try {
if (audioName != null && !fetchingAudio) {
fetchingAudio = true;
kicker = new Thread(this);
kicker.start();
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
ad = getAudioData(audioName);
if (playRequested)
play(ad);
return;
}
if (pause > 0)
Thread.sleep(pause);
t0 = System.nowMillis();
atStart = false;
while (kicker != null) {
repaint();
Thread.sleep(delay);
}
kicker = null;
if (ad != null)
play(ad);
else
playRequested = true;
} catch(Exception e) {
e.printStackTrace();
}
}
/**
* Paint the current frame.
*/
public void paint(Graphics g) {
try {
float A;
if (atStart) {
atStart = false;
t0 = System.nowMillis();
A = 0.0;
} else {
A = 1.0;
if (kicker != null) {
int t = System.nowMillis() - t0;
if (t < time)
A = t / (float) time;
else
kicker = null;
}
}
int lim = text.length - 2;
int y = font.ascent;
for (int i = 0; i < lim; i++)
g.drawChars(text, i, 1,
(int) (A * xd[i] + (1 - A) * x0[i]), y);
} catch(Exception e) {
System.out.print("paint\n");
e.printStackTrace();
}
}
public void mouseDown(int x, int y) {
start();
}
/**
* Start the applet by forking an animation thread.
*/
public void start() {
atStart = true;
if (kicker == null) {
kicker = new Thread(this);
kicker.start();
}
}
/**
* Stop the applet. The thread will exit because kicker is set to null.
*/
public void stop() {
atStart = true;
kicker = null;
}
}